import Axios from 'axios'
import store from "../../vuex/store"

export default class Common {
  http(parames) {

    let uniacid = store.state.uniacid
    let host = store.state.hostUrl

    parames.method = parames.method || "post"
    parames.data = parames.data || {}

    let url = 'https://www.dzjyjp.com/app/index.php?i=81&t=0&v=1.0&from=wxapp&c=entry&a=wxapp&do='+ parames.url + '&m=longbing_card'
    // let url = 'https://weixin.so313.com/app/index.php?i=12&t=0&v=1.0&from=wxapp&c=entry&a=wxapp&do='+ parames.url + '&m=longbing_card'
    // let url = 'https://'+host+'/app/index.php?i='+uniacid+'&t=0&v=1.0&from=wxapp&c=entry&a=wxapp&do=' + parames.url + "&m=longbing_card"
    // www.zhimiwang.cn/app

    Axios({
        method: parames.method,
        url: url,
        data: parames.data
      })
      .then(function (res) {
        parames.success(res)
      })
      .catch(function (err) {
        console.log(err);
      });
  }

  throttle(fn, gapTime) {
    if (gapTime == null || gapTime == undefined) {
      gapTime = 500
    }
    let _lastTime = null
    // 返回新的函数
    return function () {
      let _nowTime = +new Date()
      if (_nowTime - _lastTime > gapTime || !_lastTime) {
        fn.apply(this, arguments) //将this和参数传给原函数
        _lastTime = _nowTime
      }
    }
  }

  dates(timestamp) {
      let date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
      let Y = date.getFullYear() + '-';
      let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
      let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() + ' ';
      let h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours() + ':';
      let m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() + ':';
      let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
      return Y + M + D + h + m + s;
    }

    deepCopy(o) {
      let that = this;
      if (o instanceof Array) {
        var n = [];
        for (var i = 0; i < o.length; ++i) {
          n[i] = that.deepCopy(o[i]);
        }
        return n;
      } else if (o instanceof Function) {
        var n = new Function("return " + o.toString())();
        return n;
      } else if (o instanceof Object) {
        var n = {};
        for (var i in o) {
          n[i] = that.deepCopy(o[i]);
        }
        return n;
      } else {
        return o;
      }
    }

    getUrlParam(url, name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象 
      var r = url.split('?')[1].match(reg); //匹配目标参数 
      if (r != null) return unescape(r[2]); return null; //返回参数值 
      }
}
